// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin 1746365080@qq.com
 * @Date: 2024-11-05 16:55:20
 * @LastEditors: qiulin 1746365080@qq.com
 * @LastEditTime: 2024-12-23 14:53:22
 * @FilePath: /SGSim/Import/BDFImport/BulkData/Load/QBDY3Parser.cpp
 */

#include "Import/BDFImport/BulkData/Load/QBDY3Parser.h"

#include <algorithm>
#include <cstdint>
#include <vector>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Load/IQBDY3Service.h"

#include "Import/BDFImport/BDFParserBase.h"
BULKDATA_ENTRY_PARSER_REG (QBDY3);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/QBDY3/QBDY3.md>
*/
// 以下需要修改
namespace
{

    std::vector<std::string> getEids (std::vector<std::string>& dividedEntryInfo)
    {
        std::vector<std::string> eids;
        eids.reserve (dividedEntryInfo.size ());
        size_t const allRowNum  = dividedEntryInfo.size () / 10;
        size_t       beginIndex = 4;
        // 遍历每一行条目信息
        for (size_t curRow = 0; curRow < allRowNum; curRow++)
        {
            if (curRow != 0)
            {
                beginIndex = 1;
            }

            // 遍历当前行的每一个字段
            for (size_t fieldsColumn = beginIndex; fieldsColumn < 9; fieldsColumn++)
            {
                // 获取当前字段的索引
                const size_t curIndex = curRow * 10 + fieldsColumn;
                eids.push_back (dividedEntryInfo[curIndex]);
            }
        }
        return eids;
    }
}  // namespace

SG::DataStructure::Common::Status BDF::BULKDATA::ParseQBDY3 (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                               dividedEntryInfo)
{
    // 创建一个用于保存QBDY3数据的临时结构体对象
    SG::DataStructure::FEM::QBDY3Data tempQBDY3;

    // TODO(bug 001) 填写m_domainId的正确值
    // 目前m_domainId硬编码为1，需要进一步确认是否正确
    tempQBDY3.m_domainId = 1;

    tempQBDY3.m_sId = convertToI64 (dividedEntryInfo[1]);
    tempQBDY3.m_Q0  = convertToReal (dividedEntryInfo[2]);

    bool isEmpty = is_all_space (dividedEntryInfo[3]);
    if (!isEmpty)
    {
        tempQBDY3.m_CNTRLND = convertToI64 (dividedEntryInfo[3]);
    }

    std::vector<std::string> EIDS = getEids (dividedEntryInfo);
    for (size_t i = 0; i < EIDS.size (); i++)
    {
        isEmpty = is_all_space (EIDS[i]);
        if (isEmpty)
        {
            continue;
        }
        SG::DataStructure::Common::I64 beging = convertToI64 (EIDS[i]);
        SG::DataStructure::Common::I64 end    = beging + 1;
        SG::DataStructure::Common::I64 step   = 1;

        if (boost::icontains (EIDS[i + 1], "THRU"))
        {
            end = convertToI64 (EIDS[i + 2]);
            i += 2;
        }

        if (boost::icontains (EIDS[i + 1], "BY"))
        {
            step = convertToI64 (EIDS[i + 2]);
            i += 2;
        }

        for (SG::DataStructure::Common::I64 curEind = beging; curEind < end; curEind += step)
        {
            tempQBDY3.m_EID = curEind;
            if (dbServiceFactory->get<IQBDY3Service> ()->Add (tempQBDY3) == Common::Status::STATUS_Failure)
            {
                throw std::runtime_error ("Failed to add SPOINT data to database.");
                return Common::Status::STATUS_Failure;
            }
        }
    }

    return Common::Status::STATUS_Success;
}